from mainfunc.baseapi import BaseApi
from config.fileName import title_dic_sh


class HandleFilesh(BaseApi):
    def __init__(self):
        self.url = "http://www.sse.com.cn/services/tradingtech/data/"
        self.req = BaseApi.getSource(self.url)

    def downF(self):
        for file_name in list(title_dic_sh.keys()):
            partten = f'</span><a href="/services/tradingtech/data/(\S*.[pdf|xlsx]?)" title="IS(\d+)_{file_name}'
            try:
                self.download_name = BaseApi.searchName(partten, self.req)
                self.download_url = BaseApi.combineUrl(self.url, self.download_name)
                fileTpye = self.download_name.split('.')[-1]
                BaseApi.cleanFile(title_dic_sh[file_name])
                BaseApi.downFile(fileTpye, file_name, self.download_url)
                print(f'文件:{file_name}处理成功')
            except Exception as e:
                print(f'文件:{file_name}处理失败,错误信息：{e}')
